package com.lyl.springcloud.controller;

import com.lyl.springcloud.server.PaymentFeignServer;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author lylbell
 * @date 2021-05-28 16:59
 */
@RestController
//若方法上有注解@HystrixCommand,并且没有fallbackMethod属性，则出问题默认调用payment_global_fallbackMethod降级方法
@DefaultProperties(defaultFallback = "payment_global_fallbackMethod")
public class OrderController {

    @Resource
    private PaymentFeignServer paymentFeignServer;

    //访问正常
    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_ok(@PathVariable("id") Integer id) {
        return paymentFeignServer.paymentInfo_ok(id);
    }

    //访问超时
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
/*    @HystrixCommand(fallbackMethod = "consumerOrder_handler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })*/
    @HystrixCommand
    public String paymentInfo_timeout(@PathVariable("id") Integer id) {
        return paymentFeignServer.paymentInfo_timeout(id);
    }

    public String consumerOrder_handler(Integer id) {
        return "消费方80，线程池：" + Thread.currentThread().getName() + "服务器繁忙，请稍后再试,id="+id ;
    }

    public String payment_global_fallbackMethod() {
        return "全局fallback降级处理";
    }
}
